package com.shujia.util

import org.apache.flink.configuration.Configuration
import org.apache.flink.contrib.streaming.state.RocksDBStateBackend
import org.apache.flink.runtime.state.StateBackend
import org.apache.flink.streaming.api.CheckpointingMode
import org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup
import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.api.bridge.scala._
import org.apache.flink.table.catalog.hive.HiveCatalog


abstract class FlinkTool {

  var env: StreamExecutionEnvironment = _
  var table: StreamTableEnvironment = _

  def main(args: Array[String]): Unit = {
    println("开始运行flink程序")
    env = StreamExecutionEnvironment.getExecutionEnvironment


    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //指定计划器为blink
      .inStreamingMode() //指定流模式
      .build()

    /**
      *
      * 构建flink table环境
      *
      */
    table = StreamTableEnvironment.create(env, bsSettings)


    /**
      * 创建hive元数据存储
      *
      */

    // Create a HiveCatalog
    val catalog = new HiveCatalog("myhive", null, "/usr/local/soft/hive-1.2.1/conf")

    // Register the catalog
    table.registerCatalog("myhive", catalog)

    //切换  catalog
    table.useCatalog("myhive")


    /**
      * 开始sql的动态选项
      *
      */

    val configuration = new Configuration
    configuration.setString("table.dynamic-table-options.enabled", "true")
    table.getConfig.addConfiguration(configuration)


    //开启checkpoint
    this.doCheckpoint()



    //调用抽象方法
    //调用了子类的run方法
    this.run(args)

  }

  /**
    * 需要在子类实现抽象方法
    *
    */
  def run(args: Array[String])


  def doCheckpoint(): Unit = {

    // 每 1000ms 开始一次 checkpoint
    env.enableCheckpointing(5000)

    // 高级选项：

    // 设置模式为精确一次 (这是默认值)
    env.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE)

    // 确认 checkpoints 之间的时间会进行 500 ms
    env.getCheckpointConfig.setMinPauseBetweenCheckpoints(500)

    // Checkpoint 必须在一分钟内完成，否则就会被抛弃
    env.getCheckpointConfig.setCheckpointTimeout(60000)

    // 同一时间只允许一个 checkpoint 进行
    env.getCheckpointConfig.setMaxConcurrentCheckpoints(1)

    // 开启在 job 中止后仍然保留的 externalized checkpoints
    env.getCheckpointConfig.enableExternalizedCheckpoints(ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION)

    /**
      * 使用RockDb作为状态后端
      *
      * 1、状态会先保存到task所在节点的硬盘上，checkpoint的时候会将状态数据持久化到hdfs
      *
      *
      * 可以使用增量快照
      *
      */

    //使用hdfs作为状态后端
    val stateBackend: StateBackend = new RocksDBStateBackend("hdfs://master:9000/flink/gma/checkpoint", true)

    env.setStateBackend(stateBackend)

  }


}
